package com.example.biye;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import android.database.sqlite.SQLiteOpenHelper;
import com.example.biye.bean.UserInfo;
import com.example.biye.database.UserDBHelper;
import android.bluetooth.BluetoothSocket;

import java.util.ArrayList;

public class CeliangActivity extends AppCompatActivity {
    private TextView tv_sqlite;
    private UserDBHelper mHelper;




    private void readSQLite() {
        if (mHelper == null) {
            Toast.makeText(CeliangActivity.this, "数据库连接为空", Toast.LENGTH_SHORT).show();
            return;
        }
        // 执行数据库帮助器的查询操作
        ArrayList<UserInfo> userArray = mHelper.query("1=1");
        String desc = String.format("数据库查询到%d条记录，详情如下：", userArray.size());
        for (int i = 0; i < userArray.size(); i++) {
            UserInfo info = userArray.get(i);
            desc = String.format("%s\n第%d条记录信息如下：", desc, i + 1);
            desc = String.format("%s\n　心率为%d", desc, info.xinlv);
            desc = String.format("%s\n　血氧为%d", desc, info.xueyang);
            desc = String.format("%s\n　更新时间为%s", desc, info.update_time);
        }
        if (userArray.size() <= 0) {
            desc = "数据库查询到的记录为空";
        }
        tv_sqlite.setText(desc);
    }


    @Override
    protected void onStop() {
        super.onStop();
        // 关闭数据库连接
        mHelper.closeLink();
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_celiang);
        getSupportActionBar().hide();
        tv_sqlite = findViewById(R.id.tv_sqlite);
        mHelper = UserDBHelper.getInstance(this, 3);
        Button buttonchaxun = (Button) findViewById(R.id.buttonchaxun);
        Button buttonqingkong = (Button) findViewById(R.id.buttonqingkong);



        buttonchaxun.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mHelper.openReadLink();
                        readSQLite();
                    }
                });
        buttonqingkong.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mHelper.closeLink();
                        // 打开数据库帮助器的写连接
                        mHelper.openWriteLink();
                        // 删除所有记录
                        mHelper.deleteAll();
                        // 关闭数据库连接
                        mHelper.closeLink();
                        // 打开数据库帮助器的读连接
                        mHelper.openReadLink();
                        readSQLite();
                    }
                });

    }
}